package Client.GUI;

import java.awt.Color;
import java.util.ArrayList;

import Client.logic.ClientIF;
import Server.DataBase.Request;
import Server.GUI.MyJTable;
import Server.Message.MessageApproveRequest;
import Server.Message.MessageDismissRequest;
import Server.Message.MessageGetRequest;
import Server.Message.MessageGetRequestReplay;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.JTable;
import java.awt.Font;
import java.io.File;
import java.io.IOException;

public class RequestsPanel extends MyJPanel {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private RequestTableModel rtm;
        private ArrayList<Request> arrayRequests =null;
        private MyJTable table;

        public RequestsPanel(ClientIF client) {
                super(PanelType.REQUEST_PANEL, client);
                setLayout(null);
                BufferedImage myPic;
        		try {
        			myPic = ImageIO.read(new File("images"+ File.separator+"requests.jpg"));
        		
        			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
        			lblAddClass.setBounds(68, -8, 699, 99);
        			add(lblAddClass);
        		
        		} catch (IOException e) {
        			e.printStackTrace();
        		}
                init();
        }
        
        public void init(){
                initArrays();
                
                JButton btnApprove = new JButton("Approve");
                btnApprove.setBounds(215, 491, 97, 25);
                btnApprove.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                Request req = arrayRequests.remove(table.getSelectedItem());
                                rtm.setArray(arrayRequests);
                                getClient().sendMsgToServer(new MessageApproveRequest(req));
                        }
                });
                add(btnApprove);
                
                JButton btnDismiss = new JButton("Dismiss");
                btnDismiss.setBounds(570, 491, 97, 25);
                btnDismiss.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                Request req1 = arrayRequests.remove(table.getSelectedItem());
                                rtm.setArray(arrayRequests);
                               getClient().sendMsgToServer(new MessageDismissRequest(req1));
                        }
                });
                add(btnDismiss);
                
                initTables();
        }
        public void initTables(){
                rtm = new RequestTableModel();          
                rtm.setArray(arrayRequests);
                table = new MyJTable(rtm);
                table.getTable().setFont(new Font("Tahoma", Font.PLAIN, 11));
                table.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
                table.setBounds(12, 104, 819, 335);
                table.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent e) {
                                table.selectedRow = table.getTable().getSelectedRow();
                        }
                });
                table.getTable().setBounds(2, 31, 572, 302);
                table.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
                table.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                add(table); 
                
        }
        
        public void initArrays(){
                getClient().sendMsgToServer(new MessageGetRequest());
                MessageGetRequestReplay msg = (MessageGetRequestReplay) getClient().getMessageFromServer();
                arrayRequests = msg.getArray();
        
        }

        public MyJPanel pushPanel() {
                return new RequestsPanel(getClient());
        }
}
